<!doctype html>
<html  class="x-admin-sm">
<head>
	<meta charset="UTF-8">
	<title>VIP用户（注册）</title>
    <link rel="stylesheet" href="css/registered.css">
    <script src="js/jquery-3.4.1.js"></script>
    <script>

        var init_down_time = 60;			    /*倒计时总时长*/
        var down_time = 60;				/*倒计时变量*/
        function set_time(val)
        {
            if(down_time == 0) {
                val.removeAttribute("disabled");    	/*移除框的属性*/
                val.innerHTML="获取验证码";
                down_time = init_down_time;
            }else{
                val.setAttribute("disabled",true); 		/*设置框的属性*/
                val.innerHTML="重新发送(" + down_time + ")";
                down_time--;

                setTimeout(function(){set_time(val)},1000);   	/*设置一秒*/
            }
        }

        function login(){
            location.href="login.html";
        }

        function registered(){
            let username = document.getElementById("username").value;
            if (username == ""){
                let use = document.getElementById("return");
                use.innerHTML = "账号不能为空";
            }else {
                let use = document.getElementById("return");
                use.innerHTML = "";
            }
            let password = document.getElementById("password").value;
            if (username == ""){
                let use = document.getElementById("return");
                use.innerHTML = "密码不能为空";
            }else {
                let use = document.getElementById("return");
                use.innerHTML = "";
            }
            let email = document.getElementById("email").value;
            if (email == ""){
                let use = document.getElementById("return");
                use.innerHTML = "邮箱不能为空";
            }else {
                let use = document.getElementById("return");
                use.innerHTML = "";
            }
            let verification = document.getElementById("verification").value;
            if (verification == ""){
                let use = document.getElementById("return");
                use.innerHTML = "验证码不能为空";
            }else {
                let use = document.getElementById("return");
                use.innerHTML = "";
            }
            $.post({
                url:"/registered",
                data:{"username":username,
                    "password":password,
                    "email":email,
                    "verification":verification
                },
                success:function (data){
                    if (data.toString() == "true"){
                        location.href="login.html";
                    }else {
                        console.log("fallse")
                        let ret = document.getElementById("return");
                        ret.innerHTML = "验证码错误";
                    }
                }
            });
        }

        function send(){
            let username = document.getElementById("username").value;
            let email = document.getElementById("email").value;
            if (email == ""){
                let use = document.getElementById("return");
                use.innerHTML = "邮箱不能为空";
            }else if (email.match("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$") == null ){
                let use = document.getElementById("return");
                use.innerHTML = "邮箱格式不正确";
            }else {
                let use = document.getElementById("return");
                use.innerHTML = "";
                $.post({
                    url: "/verification",
                    data: {"email":email,
                        "username":username},
                    success:function (data){
                        if (data.toString() == "true"){
                            let send = document.getElementById("send");
                            set_time(send)
                        }
                    }
                });
            }
        }



    </script>
</head>
<body class="login-bg">

    <div class="login">
        <div class="message">会员注册</div>
        <div id="darkbannerwrap"></div>
            <input id="username" name="username" placeholder="用户名"
                   type="text" lay-verify="username" class="layui-input">
            <hr class="hr15">
            <input id="password" name="password" placeholder="密码"
                   lay-verify="passwd"  type="password" class="layui-input">
            <hr class="hr15">
            <input id="email" name="email" placeholder="邮箱"
               lay-verify="email"  type="email" class="layui-input">
            <hr class="hr15">
            <input id="verification" name="verification" placeholder="验证码"
                   lay-verify="verification"  type="text" class="layui-input">
            <button id="send" type="submit" onclick="send()">发送验证码</button>
            <hr class="hr15">
            <hr class="hr15">
            <input value="注册" lay-submit lay-filter="login" style="width:100%;" type="submit" onclick="registered()">
            <hr class="hr20" >
            <span id="return" style="color: red;width: 150px;margin: 0"></span>
            <button id="registered" type="submit" onclick="login()">登录</button>
    </div>

</body>
</html>